'use strict';

const { data } = require('mm');

module.exports = (app) => {
  class ClassifyService extends app.Service {
    // 创建分类
    async create() {
      const { app, ctx } = this;
      const { Classify } = app.model.Tables.Blog;
      const data = ctx.request.body;
      // 创建分类信息
      const res = await Classify.create({
        classifyName: data.classifyName,
        projectId: ctx.adminInfo.project_id,
      });
      return res;
    }

    // 删除分类
    async delete() {
      const { app, ctx } = this;
      const { Classify } = app.model.Tables.Blog;
      const { id } = ctx.params;

      // 删除分类信息
      const res = await Classify.destroy({
        where: {
          id,
          projectId: ctx.adminInfo.project_id,
        },
      });
      console.log(res);
      return res;
    }
    // 获取分类信息
    async get() {
      const { app, ctx } = this;
      const { Classify } = app.model.Tables.Blog;
      const data = ctx.query;
      const res = Classify.findAndCountAll({
        where: {
          projectId: data.projectId,
        },
      });
      return res;
    }

    // 修改分类
    async update() {
      const { app, ctx } = this;
      const { Classify } = app.model.Tables.Blog;
      const data = ctx.request.body;
      const { id } = ctx.params;

      // 删除分类信息
      const res = await Classify.update(
        {
          classifyName: data.classifyName,
        },
        {
          where: {
            id,
            projectId: ctx.adminInfo.project_id,
          },
        }
      );
      console.log(res);
      return res;
    }
  }
  return ClassifyService;
};
